/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package lib.model;

import lib.xml.XMLizable;
import java.util.Calendar;
import lib.exceptions.InvalidLengthException;

/**
 * The class interval describes a time interval between two dates.
 * @author Jorge Mira Yagüe
 */
public class Interval implements XMLizable {

    private Calendar start;
    private Calendar end;

    /**
     * Builds an empty interval
     */
    public Interval() {
        start = null;
        end = null;
    }

    /**
     * Builds an interval from a start and end date
     * @param start the start date of the interval
     * @param end the end date of the interval
     * @throws InvalidLengthException when the start date is not before than the
     * end date
     */
    public Interval(Calendar start, Calendar end)
            throws InvalidLengthException {
        this.start = start;
        this.end = end;
        if (!start.before(end)) {
            throw new InvalidLengthException();
        }
    }

    /**
     * Builds an interval from a start date, a length in milliseconds
     * @param start the start date of the interval
     * @param length the length in millseconds of the interval
     * @throws InvalidLengthException when the length is lesser or equal to zero
     */
    public Interval(Calendar start, double length)
            throws InvalidLengthException {
        if (length <= 0) {
            throw new InvalidLengthException();
        }
        this.start = start;
        end = (Calendar) this.start.clone();
        end.add(Calendar.MILLISECOND, (int) length);
    }

    /**
     * Copy constructor
     * @param interval the interval to make a copy
     */
    public Interval(Interval interval) {
        this.start = interval.start;
        this.end = interval.end;
    }

    /**
     * Returns true if the interval contains the calendar
     * @param calendar the calendar to be checked if is contained
     * @return true if the interval contains the calendar
     */
    public boolean contains(Calendar calendar) {
        return (!start.after(calendar) && !end.before(calendar));
    }

    /**
     * Returns true if the interval contains the interval given
     * @param interval the interval to be checked if is contained
     * @return true if the interval contains the interval given
     */
    public boolean contains(Interval interval) {
        return (contains(interval.start) && contains(interval.end));
    }

    /**
     * Returns true if the interval overlaps the interval given
     * @param interval the interval to be checked if is overlapped
     * @return true if the interval overlaps the interval given
     */
    public boolean overlaps(Interval interval) {
        return (contains(interval.start) ||
                contains(interval.end) ||
                interval.contains(this));
    }

    /**
     * Returns the length in milliseconds of the interval
     * @return the length in milliseconds of the interval
     */
    public double Length() {
        return end.getTime().getTime() - start.getTime().getTime();
    }

    public Calendar getStart() {
        return start;
    }

    public Calendar getEnd() {
        return end;
    }

    public String toXML() {
        String result = "<Interval ";
        result += "start=\"" + start.getTimeInMillis() + "\" ";
        result += "end=\"" + end.getTimeInMillis() + "\" ";
        result += "/>\n";
        return result;
    }

    @Override
    public String toString() {
        return start.get(Calendar.DATE) + "/" +
                start.get(Calendar.MONTH) + "/" +
                start.get(Calendar.YEAR) + " " +
                start.get(Calendar.HOUR_OF_DAY) + ":" +
                start.get(Calendar.MINUTE) + ":" +
                start.get(Calendar.SECOND) + "." +
                start.get(Calendar.MILLISECOND) + " - " +
                end.get(Calendar.DATE) + "/" +
                end.get(Calendar.MONTH) + "/" +
                end.get(Calendar.YEAR) + " " +
                end.get(Calendar.HOUR_OF_DAY) + ":" +
                end.get(Calendar.MINUTE) + ":" +
                end.get(Calendar.SECOND) + "." +
                end.get(Calendar.MILLISECOND);
    }
}
